define(function(require, exports, module) {

	var $ = require('jquery');
	var List = require('../util/list');
	require('artTemplate/artTemplate/2.0.1/template.js');
	require('seajs/seajs/2.1.1/sea.js');
	require('jquery/jquery-form/1.0.0/jquery.form.js');
	require('jquery/jquery-validation/1.11.0/jquery.validate');
	require('jquery/jquery-validation/1.11.0/messages_zh');
	require('jbox');
	require("../util/jquery-validate-extends")
	
	exports.options = {
		listLink : '',
		listParams : '',
		pageLink : '',
		pageParams : ''
	};
	var v = $('.nav-tabs .active').attr("id");
	
	//添加删除函数	
	exports.del=function(e){
		if(e=="admin"&&g_user.role=="admin"){
			$(".edit").attr({style:"display:none"});
		}else{
		$(".icon-trash").on("click",function(){
			var id=$(this).attr("data");
			//console.log(id);
			exports.options.deleteUserLink=$CONFIG.base_url+"/api/admin/user/delete";
			exports.options.deleteUserParams={"uid":id};
			$.jBox("删除后将无法恢复，确定删除该用户吗？", {title: "友情提示",buttons:{"确定":1,"取消":-1},submit:function(v,h,f){
				if(v=="1"){
					var delUser=$.post(exports.options.deleteUserLink, exports.options.deleteUserParams);
					delUser.done(function(data){
						if(data=="1"){
							$.jBox.tip("删除失败,不能删除该用户！");
						}else if(data=="0"){
							exports.showUserList(e);
							$.jBox.tip("删除成功！");
						}
					});
					return true;
				}
				if(v=="-1"){
					$.jBox.close();
					return false;
				}
			}});
			
			});
		};
	};
	
	exports.showUserList = function showUserList(e) {
		var userList = new List();
		userList.options.template = "userList-template";
		userList.options.container = "#userList";
		userList.options.links = { "list" : $CONFIG.base_url + "/api/user/list" };
		userList.options.params = {"role" : e,"s":10};
		userList.load(function(){
			exports.del(e);
		});
				
	};
	
	exports.searchUserList = function searchUserList(by,condition) {
		var userList = new List();
		userList.options.template = "userList-template";
		userList.options.container = "#userList";
		userList.options.links = { "list" : $CONFIG.base_url + "/api/user/fileterList" };
		userList.options.params = {"by" : by,"condition":condition};
		userList.load();

	};
	
	//选择角色类型列表
	exports.showRolesTypeList = function showRolesTypeList() {
		var getting = $.get($CONFIG.base_url + "/api/roles");
		getting.done(function(data) {
			$.each(data,function(key,val){
				var $option = $("<option id='" + val.id + "' value= '"+val.id+"'>"+ val.roleName + "</option>");
				$("#roles_select").append($option);
			});
		});
		
	};
	
	$(document).ready(function(data) {
		exports.showUserList("user");
		$("#add-form").validate({
			rules : {
				loginName : {
					required : true,
					remote :{
						url:"regist/check_login_name",
					},
					ismobile : true
				},
				name:{
					required:true,
					maxlength:20,
					remote:{
						url:"regist/check_name",
					},
				},
				plainPassword:{
					required:true,
					rangelength:[6,18],
				}
			},
			messages : {
				loginName : {
					required:function(){
						$.jBox.tip("手机号不能为空!");
					},
					remote:function(){
						$.jBox.tip("手机号已注册");
					},
					ismobile:function(){
						$.jBox.tip("手机号必须为正确格式!");
					}
				},
				name:{
					required:function(){
						$.jBox.tip("昵称不能为空。");
					},
					maxlength:function(){
						$.jBox.tip("昵称最多20字符。");

					},
					remote:function(){
						$.jBox.tip("昵称已占用.");
					}
				},
				plainPassword:{
					required:function(){
						$.jBox.tip("密码名不能为空!");
					},
					rangelength:function(){
						$.jBox.tip("密码为6-18位！");
					},
				}
			},
		});
			
			$('.add-submit').click(function(){
				if(!$("#add-form").valid()){
					return;
				};
				var postData = form2js('add-form', '.', true);
				postData["roles"]={"id":postData["rolesId"]};
				delete postData["rolesId"];
				//console.log(postData);
				$.ajax({
					url : $CONFIG.base_url + "/api/admin/user/add",
					type : "POST",
					data : $.toJSON(postData),
					dataType : "json",
					contentType : "application/json",
					success : function() {
						$.jBox.tip("添加新用户成功！");
						//setTimeout(function(){window.location.href=$CONFIG.base_url + '/'+$CONFIG.admin_url+'/manage/project';},1000);
						setTimeout(function(){window.location.href=$CONFIG.base_url + '/'+$CONFIG.admin_url;},1000);;
					},
					error : function() {
						$.jBox.tip("添加新用户失败！");
					}
				});
			});
			exports.showRolesTypeList();
	});
	
	$("#name-search").click(function(){
		var by = $(this).attr("name");
		var condition = $(this).parent().children("input[name=name-text]").val();
		exports.searchUserList(by,condition);
		$("input[name=phone-text]").attr("value",'');
	});
	$("#phone-search").click(function(){
		var by = $(this).attr("name");
		var condition = $(this).parent().children("input[name=phone-text]").val();
		exports.searchUserList(by,condition);
		$("input[name=name-text]").attr("value",'');
	});
	
	
	//用户历史消费详情
	exports.detail = function detail(uid,date) {
		var fundDetails = new List();
		fundDetails.options.template = "fund-table-template";
		fundDetails.options.container = "#userList";
		fundDetails.options.links = {
			"list" : $CONFIG.base_url + '/api/fund_detail/list'
		};
		fundDetails.options.params = {
			"uid" : uid,
			"p" : 1,
			"s" : 10
		};
		fundDetails.load(
				function(){
					$(".hf").off();
					$(".hf").on("click",function(){
						var role = $("#tabs-598782 li.active").attr("id");
						exports.showUserList(role);
					});
				});
	};

	
	$("#pt_user").click(function() {
		exports.showUserList("user");
		$("input[name=name-text]").attr("value",'');
		$("input[name=phone-text]").attr("value",'');
	});
	$("#jg_user").click(function() {
		exports.showUserList("institution");
		$("input[name=name-text]").attr("value",'');
		$("input[name=phone-text]").attr("value",'');
	});
	$("#org_user").click(function() {
		exports.showUserList("charitarian");
	});
	$("#admin_user").click(function() {
		exports.showUserList("admin");
		$("input[name=name-text]").attr("value",'');
		$("input[name=phone-text]").attr("value",'');
	});
	
	$("#userList").on('click',"#history-finance",function(){
		var uid=$(this).attr("data");
		exports.detail(uid,15);
	});
	
	
});